package io.renren.modules.bus.service.impl;

import io.renren.modules.bus.dao.MemberDao;
import io.renren.modules.bus.entity.MemberEntity;
import io.renren.modules.bus.entity.OptometryRecordEntity;
import io.renren.modules.bus.vo.CostRecordVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.bus.dao.CostRecordDao;
import io.renren.modules.bus.entity.CostRecordEntity;
import io.renren.modules.bus.service.CostRecordService;


@Service("costRecordService")
public class CostRecordServiceImpl extends ServiceImpl<CostRecordDao, CostRecordEntity>
        implements CostRecordService {

    @Autowired
    private CostRecordDao dao;
    @Autowired
    private MemberDao memberDao;
    /**
     * 单表分页查询
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<CostRecordEntity> page = this.selectPage(
                new Query<CostRecordEntity>(params).getPage(),
                new EntityWrapper<CostRecordEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 分页查询
     */
    @Override
    public PageUtils list4Page(Map<String, Object> params){
        Page<CostRecordEntity> page = new Query<CostRecordEntity>(params).getPage();
        return new PageUtils(page.setRecords(dao.list4Page(page, params)));
    }

    /**
     * 条件查询
     */
    public List<CostRecordEntity> listByCondition(Map<String, Object> params) {
        return dao.listByCondition(params);
    }

    /**
     * 逻辑删除多条记录
     */
    public Boolean deleteLogic(List ids) {
        return dao.deleteLogic(ids);
    }

    @Override
    public CostRecordEntity queryOneById(Integer id) {
        return dao.queryOneById(id);
    }

    /**
     * 根据id查询详情
     */
    @Override
    public CostRecordVO queryOne(Integer id) {
        return dao.queryOne(id);
    }

    @Override
    public void save(CostRecordVO costRecordVO, Integer type) {
        CostRecordEntity costRecordEntity = new CostRecordEntity();

        //会员积分及消费累加
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setId(costRecordVO.getMemberId());
        if (1 == type) {
            costRecordVO.setCurrentPoint(costRecordVO.getCurrentPoint()+costRecordVO.getChangePoint());
            memberEntity.setMonetary(costRecordVO.getMonetary().add(costRecordVO.getActuallyPaid()));

            BeanUtils.copyProperties(costRecordVO, costRecordEntity);
            dao.insert(costRecordEntity);
        } else if (2 == type) {
            costRecordVO.setCurrentPoint(costRecordVO.getCurrentPoint()-costRecordVO.getChangePoint_Q()+costRecordVO.getChangePoint());
            memberEntity.setMonetary(costRecordVO.getMonetary().subtract(costRecordVO.getActuallyPaid_Q()).add(costRecordVO.getActuallyPaid()));

            BeanUtils.copyProperties(costRecordVO, costRecordEntity);
            dao.updateById(costRecordEntity);
        }
        memberEntity.setCurrentPoint(costRecordVO.getCurrentPoint());
        memberDao.updateById(memberEntity);
    }

}
